package common

import (
	"jlpay-sdk-go/pkg/jlpay/core"
	"jlpay-sdk-go/pkg/jlpay/models"
)

// ==================== 撤销请求 ====================

// CancelRequest 撤销请求
type CancelRequest struct {
	models.OrgBaseRequest
	// MchID 商户号
	MchID string `json:"mch_id,omitempty"`
	// OutTradeNo 外部订单号
	OutTradeNo string `json:"out_trade_no,omitempty"`
	// OriOutTradeNo 原外部订单号
	OriOutTradeNo string `json:"ori_out_trade_no,omitempty"`
	// TotalFee 交易金额
	TotalFee string `json:"total_fee,omitempty"`
	// OriTransactionID 原平台订单号
	OriTransactionID string `json:"ori_transaction_id,omitempty"`
	// Remark 备注
	Remark string `json:"remark,omitempty"`
	// MchCreateIP 终端IP
	MchCreateIP string `json:"mch_create_ip,omitempty"`
	// Longitude 经度
	Longitude string `json:"longitude,omitempty"`
	// Latitude 纬度
	Latitude string `json:"latitude,omitempty"`
	// 原始请求映射数据
	rawRequestMap map[string]interface{} `json:"-"`
}

// NewCancelRequest 创建撤销请求
func NewCancelRequest() *CancelRequest {
	return &CancelRequest{}
}

// SetMchID 设置商户号
func (r *CancelRequest) SetMchID(mchID string) *CancelRequest {
	r.MchID = mchID
	return r
}

// SetOutTradeNo 设置外部订单号
func (r *CancelRequest) SetOutTradeNo(outTradeNo string) *CancelRequest {
	r.OutTradeNo = outTradeNo
	return r
}

// SetOriOutTradeNo 设置原外部订单号
func (r *CancelRequest) SetOriOutTradeNo(oriOutTradeNo string) *CancelRequest {
	r.OriOutTradeNo = oriOutTradeNo
	return r
}

// SetTotalFee 设置交易金额
func (r *CancelRequest) SetTotalFee(totalFee string) *CancelRequest {
	r.TotalFee = totalFee
	return r
}

// SetOriTransactionID 设置原平台订单号
func (r *CancelRequest) SetOriTransactionID(oriTransactionID string) *CancelRequest {
	r.OriTransactionID = oriTransactionID
	return r
}

// SetRemark 设置备注
func (r *CancelRequest) SetRemark(remark string) *CancelRequest {
	r.Remark = remark
	return r
}

// SetMchCreateIP 设置终端IP
func (r *CancelRequest) SetMchCreateIP(mchCreateIP string) *CancelRequest {
	r.MchCreateIP = mchCreateIP
	return r
}

// SetLongitude 设置经度
func (r *CancelRequest) SetLongitude(longitude string) *CancelRequest {
	r.Longitude = longitude
	return r
}

// SetLatitude 设置纬度
func (r *CancelRequest) SetLatitude(latitude string) *CancelRequest {
	r.Latitude = latitude
	return r
}

// SetRawRequestMap 设置原始请求映射数据
func (r *CancelRequest) SetRawRequestMap(rawMap map[string]interface{}) *CancelRequest {
	r.rawRequestMap = rawMap
	return r
}

// GetRawMap 获取原始映射数据
func (r *CancelRequest) GetRawMap() (map[string]interface{}, bool) {
	return r.rawRequestMap, r.rawRequestMap != nil
}

// ToMap 转换为map
func (r *CancelRequest) ToMap() (map[string]interface{}, error) {
	// 如果有原始映射数据，直接返回
	if rawMap, exists := r.GetRawMap(); exists {
		return rawMap, nil
	}

	// 构建基础请求数据
	requestMap := make(map[string]interface{})

	// 添加基础字段
	if r.MchID != "" {
		requestMap["mch_id"] = r.MchID
	}
	if r.OutTradeNo != "" {
		requestMap["out_trade_no"] = r.OutTradeNo
	}
	if r.OriOutTradeNo != "" {
		requestMap["ori_out_trade_no"] = r.OriOutTradeNo
	}
	if r.TotalFee != "" {
		requestMap["total_fee"] = r.TotalFee
	}
	if r.OriTransactionID != "" {
		requestMap["ori_transaction_id"] = r.OriTransactionID
	}
	if r.Remark != "" {
		requestMap["remark"] = r.Remark
	}
	if r.MchCreateIP != "" {
		requestMap["mch_create_ip"] = r.MchCreateIP
	}
	if r.Longitude != "" {
		requestMap["longitude"] = r.Longitude
	}
	if r.Latitude != "" {
		requestMap["latitude"] = r.Latitude
	}

	// 合并基础请求数据
	baseMap, err := r.OrgBaseRequest.ToMap()
	if err != nil {
		return nil, err
	}

	for k, v := range baseMap {
		requestMap[k] = v
	}

	return requestMap, nil
}

// CheckRequiredParams 检查必需参数
func (r *CancelRequest) CheckRequiredParams(config *core.Config) error {
	return r.OrgBaseRequest.CheckRequiredParams(config)
}

// ==================== 撤销响应 ====================

// CancelResponse 撤销响应
type CancelResponse struct {
	models.StandardResponse
	// Status 状态 2：成功 3：失败
	Status string `json:"status,omitempty"`
	// MchID 商户号
	MchID string `json:"mch_id,omitempty"`
	// TransactionID 平台订单号
	TransactionID string `json:"transaction_id,omitempty"`
	// OutTradeNo 外部订单号
	OutTradeNo string `json:"out_trade_no,omitempty"`
	// OriOutTradeNo 原外部订单号
	OriOutTradeNo string `json:"ori_out_trade_no,omitempty"`
	// OriTransactionID 原平台订单号
	OriTransactionID string `json:"ori_transaction_id,omitempty"`
	// OrderTime 订单时间
	OrderTime string `json:"order_time,omitempty"`
	// PayType 交易类型
	PayType string `json:"pay_type,omitempty"`
	// TotalFee 交易金额
	TotalFee interface{} `json:"total_fee,omitempty"`
	// TransTime 交易时间
	TransTime string `json:"trans_time,omitempty"`
}

// NewCancelResponse 创建撤销响应
func NewCancelResponse() *CancelResponse {
	return &CancelResponse{}
}

// ==================== 退款请求 ====================

// RefundRequest 退款请求
type RefundRequest struct {
	models.OrgBaseRequest
	// MchID 商户号
	MchID string `json:"mch_id,omitempty"`
	// OutTradeNo 外部订单号
	OutTradeNo string `json:"out_trade_no,omitempty"`
	// OriOutTradeNo 原外部订单号
	OriOutTradeNo string `json:"ori_out_trade_no,omitempty"`
	// TotalFee 交易金额
	TotalFee string `json:"total_fee,omitempty"`
	// RefundFee 退款金额
	RefundFee string `json:"refund_fee,omitempty"`
	// OriTransactionID 原平台订单号
	OriTransactionID string `json:"ori_transaction_id,omitempty"`
	// Remark 备注
	Remark string `json:"remark,omitempty"`
	// MchCreateIP 终端IP
	MchCreateIP string `json:"mch_create_ip,omitempty"`
	// Longitude 经度
	Longitude string `json:"longitude,omitempty"`
	// Latitude 纬度
	Latitude string `json:"latitude,omitempty"`
	// 原始请求映射数据
	rawRequestMap map[string]interface{} `json:"-"`
}

// NewRefundRequest 创建退款请求
func NewRefundRequest() *RefundRequest {
	return &RefundRequest{}
}

// SetMchID 设置商户号
func (r *RefundRequest) SetMchID(mchID string) *RefundRequest {
	r.MchID = mchID
	return r
}

// SetOutTradeNo 设置外部订单号
func (r *RefundRequest) SetOutTradeNo(outTradeNo string) *RefundRequest {
	r.OutTradeNo = outTradeNo
	return r
}

// SetOriOutTradeNo 设置原外部订单号
func (r *RefundRequest) SetOriOutTradeNo(oriOutTradeNo string) *RefundRequest {
	r.OriOutTradeNo = oriOutTradeNo
	return r
}

// SetTotalFee 设置交易金额
func (r *RefundRequest) SetTotalFee(totalFee string) *RefundRequest {
	r.TotalFee = totalFee
	return r
}

// SetRefundFee 设置退款金额
func (r *RefundRequest) SetRefundFee(refundFee string) *RefundRequest {
	r.RefundFee = refundFee
	return r
}

// SetOriTransactionID 设置原平台订单号
func (r *RefundRequest) SetOriTransactionID(oriTransactionID string) *RefundRequest {
	r.OriTransactionID = oriTransactionID
	return r
}

// SetRemark 设置备注
func (r *RefundRequest) SetRemark(remark string) *RefundRequest {
	r.Remark = remark
	return r
}

// SetMchCreateIP 设置终端IP
func (r *RefundRequest) SetMchCreateIP(mchCreateIP string) *RefundRequest {
	r.MchCreateIP = mchCreateIP
	return r
}

// SetLongitude 设置经度
func (r *RefundRequest) SetLongitude(longitude string) *RefundRequest {
	r.Longitude = longitude
	return r
}

// SetLatitude 设置纬度
func (r *RefundRequest) SetLatitude(latitude string) *RefundRequest {
	r.Latitude = latitude
	return r
}

// SetRawRequestMap 设置原始请求映射数据
func (r *RefundRequest) SetRawRequestMap(rawMap map[string]interface{}) *RefundRequest {
	r.rawRequestMap = rawMap
	return r
}

// GetRawMap 获取原始映射数据
func (r *RefundRequest) GetRawMap() (map[string]interface{}, bool) {
	return r.rawRequestMap, r.rawRequestMap != nil
}

// ToMap 转换为map
func (r *RefundRequest) ToMap() (map[string]interface{}, error) {
	// 如果有原始映射数据，直接返回
	if rawMap, exists := r.GetRawMap(); exists {
		return rawMap, nil
	}

	// 构建基础请求数据
	requestMap := make(map[string]interface{})

	// 添加基础字段
	if r.MchID != "" {
		requestMap["mch_id"] = r.MchID
	}
	if r.OutTradeNo != "" {
		requestMap["out_trade_no"] = r.OutTradeNo
	}
	if r.OriOutTradeNo != "" {
		requestMap["ori_out_trade_no"] = r.OriOutTradeNo
	}
	if r.TotalFee != "" {
		requestMap["total_fee"] = r.TotalFee
	}
	if r.RefundFee != "" {
		requestMap["refund_fee"] = r.RefundFee
	}
	if r.OriTransactionID != "" {
		requestMap["ori_transaction_id"] = r.OriTransactionID
	}
	if r.Remark != "" {
		requestMap["remark"] = r.Remark
	}
	if r.MchCreateIP != "" {
		requestMap["mch_create_ip"] = r.MchCreateIP
	}
	if r.Longitude != "" {
		requestMap["longitude"] = r.Longitude
	}
	if r.Latitude != "" {
		requestMap["latitude"] = r.Latitude
	}

	// 合并基础请求数据
	baseMap, err := r.OrgBaseRequest.ToMap()
	if err != nil {
		return nil, err
	}

	for k, v := range baseMap {
		requestMap[k] = v
	}

	return requestMap, nil
}

// CheckRequiredParams 检查必需参数
func (r *RefundRequest) CheckRequiredParams(config *core.Config) error {
	return r.OrgBaseRequest.CheckRequiredParams(config)
}

// ==================== 退款响应 ====================

// RefundResponse 退款响应
type RefundResponse struct {
	models.StandardResponse
	// Status 状态 2：成功 3：失败
	Status string `json:"status,omitempty"`
	// MchID 商户号
	MchID string `json:"mch_id,omitempty"`
	// TransactionID 平台订单号
	TransactionID string `json:"transaction_id,omitempty"`
	// OutTradeNo 外部订单号
	OutTradeNo string `json:"out_trade_no,omitempty"`
	// OriOutTradeNo 原外部订单号
	OriOutTradeNo string `json:"ori_out_trade_no,omitempty"`
	// OriTransactionID 原平台订单号
	OriTransactionID string `json:"ori_transaction_id,omitempty"`
	// OrderTime 订单时间
	OrderTime string `json:"order_time,omitempty"`
	// PayType 交易类型
	PayType string `json:"pay_type,omitempty"`
	// TotalFee 交易金额
	TotalFee interface{} `json:"total_fee,omitempty"`
	// RefundFee 退款金额
	RefundFee interface{} `json:"refund_fee,omitempty"`
	// TransTime 交易时间
	TransTime string `json:"trans_time,omitempty"`
}

// NewRefundResponse 创建退款响应
func NewRefundResponse() *RefundResponse {
	return &RefundResponse{}
}

// ==================== 获取OpenID请求 ====================

// GetOpenidRequest 获取OpenID请求
type GetOpenidRequest struct {
	models.OrgBaseRequest
	// MchID 商户号
	MchID string `json:"mch_id,omitempty"`
	// PayType 交易类型
	PayType string `json:"pay_type,omitempty"`
	// AuthCode 授权码
	AuthCode string `json:"auth_code,omitempty"`
	// SubAppid 公众账号ID
	SubAppid string `json:"sub_appid,omitempty"`
	// AppUpIdentifier 银联支付标识
	AppUpIdentifier string `json:"app_up_identifier,omitempty"`
	// DeviceInfo 终端设备号
	DeviceInfo string `json:"device_info,omitempty"`
	// MchCreateIP 终端IP
	MchCreateIP string `json:"mch_create_ip,omitempty"`
	// 原始请求映射数据
	rawRequestMap map[string]interface{} `json:"-"`
}

// NewGetOpenidRequest 创建获取OpenID请求
func NewGetOpenidRequest() *GetOpenidRequest {
	return &GetOpenidRequest{}
}

// SetMchID 设置商户号
func (r *GetOpenidRequest) SetMchID(mchID string) *GetOpenidRequest {
	r.MchID = mchID
	return r
}

// SetPayType 设置交易类型
func (r *GetOpenidRequest) SetPayType(payType string) *GetOpenidRequest {
	r.PayType = payType
	return r
}

// SetAuthCode 设置授权码
func (r *GetOpenidRequest) SetAuthCode(authCode string) *GetOpenidRequest {
	r.AuthCode = authCode
	return r
}

// SetSubAppid 设置公众账号ID
func (r *GetOpenidRequest) SetSubAppid(subAppid string) *GetOpenidRequest {
	r.SubAppid = subAppid
	return r
}

// SetAppUpIdentifier 设置银联支付标识
func (r *GetOpenidRequest) SetAppUpIdentifier(appUpIdentifier string) *GetOpenidRequest {
	r.AppUpIdentifier = appUpIdentifier
	return r
}

// SetDeviceInfo 设置终端设备号
func (r *GetOpenidRequest) SetDeviceInfo(deviceInfo string) *GetOpenidRequest {
	r.DeviceInfo = deviceInfo
	return r
}

// SetMchCreateIP 设置终端IP
func (r *GetOpenidRequest) SetMchCreateIP(mchCreateIP string) *GetOpenidRequest {
	r.MchCreateIP = mchCreateIP
	return r
}

// SetRawRequestMap 设置原始请求映射数据
func (r *GetOpenidRequest) SetRawRequestMap(rawMap map[string]interface{}) *GetOpenidRequest {
	r.rawRequestMap = rawMap
	return r
}

// GetRawMap 获取原始映射数据
func (r *GetOpenidRequest) GetRawMap() (map[string]interface{}, bool) {
	return r.rawRequestMap, r.rawRequestMap != nil
}

// ToMap 转换为map
func (r *GetOpenidRequest) ToMap() (map[string]interface{}, error) {
	// 如果有原始映射数据，直接返回
	if rawMap, exists := r.GetRawMap(); exists {
		return rawMap, nil
	}

	// 构建基础请求数据
	requestMap := make(map[string]interface{})

	// 添加基础字段
	if r.MchID != "" {
		requestMap["mch_id"] = r.MchID
	}
	if r.PayType != "" {
		requestMap["pay_type"] = r.PayType
	}
	if r.AuthCode != "" {
		requestMap["auth_code"] = r.AuthCode
	}
	if r.SubAppid != "" {
		requestMap["sub_appid"] = r.SubAppid
	}
	if r.AppUpIdentifier != "" {
		requestMap["app_up_identifier"] = r.AppUpIdentifier
	}
	if r.DeviceInfo != "" {
		requestMap["device_info"] = r.DeviceInfo
	}
	if r.MchCreateIP != "" {
		requestMap["mch_create_ip"] = r.MchCreateIP
	}

	// 合并基础请求数据
	baseMap, err := r.OrgBaseRequest.ToMap()
	if err != nil {
		return nil, err
	}

	for k, v := range baseMap {
		requestMap[k] = v
	}

	return requestMap, nil
}

// CheckRequiredParams 检查必需参数
func (r *GetOpenidRequest) CheckRequiredParams(config *core.Config) error {
	return r.OrgBaseRequest.CheckRequiredParams(config)
}

// ==================== 获取OpenID响应 ====================

// GetOpenidResponse 获取OpenID响应
type GetOpenidResponse struct {
	models.StandardResponse
	// MchID 商户号
	MchID string `json:"mch_id,omitempty"`
	// Openid 用户在商户appid下的唯一标识
	Openid string `json:"openid,omitempty"`
	// SubOpenid 用户在子商户appid下的唯一标识
	SubOpenid string `json:"sub_openid,omitempty"`
}

// NewGetOpenidResponse 创建获取OpenID响应
func NewGetOpenidResponse() *GetOpenidResponse {
	return &GetOpenidResponse{}
}

// ==================== 渠道查询请求 ====================

// ChnQueryRequest 渠道查询请求
type ChnQueryRequest struct {
	models.OrgBaseRequest
	// MchID 商户号
	MchID string `json:"mch_id,omitempty"`
	// OutTradeNo 外部订单号
	OutTradeNo string `json:"out_trade_no,omitempty"`
	// TransactionID 平台订单号
	TransactionID string `json:"transaction_id,omitempty"`
	// 原始请求映射数据
	rawRequestMap map[string]interface{} `json:"-"`
}

// NewChnQueryRequest 创建渠道查询请求
func NewChnQueryRequest() *ChnQueryRequest {
	return &ChnQueryRequest{}
}

// SetMchID 设置商户号
func (r *ChnQueryRequest) SetMchID(mchID string) *ChnQueryRequest {
	r.MchID = mchID
	return r
}

// SetOutTradeNo 设置外部订单号
func (r *ChnQueryRequest) SetOutTradeNo(outTradeNo string) *ChnQueryRequest {
	r.OutTradeNo = outTradeNo
	return r
}

// SetTransactionID 设置平台订单号
func (r *ChnQueryRequest) SetTransactionID(transactionID string) *ChnQueryRequest {
	r.TransactionID = transactionID
	return r
}

// SetRawRequestMap 设置原始请求映射数据
func (r *ChnQueryRequest) SetRawRequestMap(rawMap map[string]interface{}) *ChnQueryRequest {
	r.rawRequestMap = rawMap
	return r
}

// GetRawMap 获取原始映射数据
func (r *ChnQueryRequest) GetRawMap() (map[string]interface{}, bool) {
	return r.rawRequestMap, r.rawRequestMap != nil
}

// ToMap 转换为map
func (r *ChnQueryRequest) ToMap() (map[string]interface{}, error) {
	// 如果有原始映射数据，直接返回
	if rawMap, exists := r.GetRawMap(); exists {
		return rawMap, nil
	}

	// 构建基础请求数据
	requestMap := make(map[string]interface{})

	// 添加基础字段
	if r.MchID != "" {
		requestMap["mch_id"] = r.MchID
	}
	if r.OutTradeNo != "" {
		requestMap["out_trade_no"] = r.OutTradeNo
	}
	if r.TransactionID != "" {
		requestMap["transaction_id"] = r.TransactionID
	}

	// 合并基础请求数据
	baseMap, err := r.OrgBaseRequest.ToMap()
	if err != nil {
		return nil, err
	}

	for k, v := range baseMap {
		requestMap[k] = v
	}

	return requestMap, nil
}

// CheckRequiredParams 检查必需参数
func (r *ChnQueryRequest) CheckRequiredParams(config *core.Config) error {
	return r.OrgBaseRequest.CheckRequiredParams(config)
}

// ==================== 渠道查询响应 ====================

// ChnQueryResponse 渠道查询响应
type ChnQueryResponse struct {
	models.StandardResponse
	// Status 状态 2：成功 3：失败
	Status string `json:"status,omitempty"`
	// MchID 商户号
	MchID string `json:"mch_id,omitempty"`
	// TransactionID 平台订单号
	TransactionID string `json:"transaction_id,omitempty"`
	// OutTradeNo 外部订单号
	OutTradeNo string `json:"out_trade_no,omitempty"`
	// ChnTransactionID 渠道订单号
	ChnTransactionID string `json:"chn_transaction_id,omitempty"`
	// OrderTime 订单时间
	OrderTime string `json:"order_time,omitempty"`
	// PayType 交易类型
	PayType string `json:"pay_type,omitempty"`
	// TotalFee 交易金额
	TotalFee interface{} `json:"total_fee,omitempty"`
	// TransTime 交易时间
	TransTime string `json:"trans_time,omitempty"`
}

// NewChnQueryResponse 创建渠道查询响应
func NewChnQueryResponse() *ChnQueryResponse {
	return &ChnQueryResponse{}
}
